#pragma once
#include "ClientState_sm.h"
class ClientsManager;
//#include "ClientsManager.h"
#include <string>
using namespace std;

DWORD WINAPI WatchDog(LPVOID lpParameter);
DWORD WINAPI WorkThread(LPVOID lpParameter);
class SkypeClient {
public:
	SkypeClient(ClientsManager* manager);
	~SkypeClient(void);

	volatile DWORD &procId();
	volatile HWND &uiWin();
	volatile HWND &ctrlWin();

	const string& callerId() const {
		return m_userName;
	}
	const string& calleeId() const {
		return m_calleeId;
	}
	const string& password() const {
		return m_password;
	}

	void setUser(const string& name, const string & password);
	void setCallee(const string& callee) {
		m_calleeId = callee;
	}
	void call();
	static void setTimeOutSeconds(int n) {
		m_timeOutSecondCount = n;
	}

	void processMessage(const string& msg);

	//smc conditions
	volatile bool isOpend() const;
	volatile bool isLogining() const;
	volatile bool isLogined() const;
	volatile bool isLogouted() const;
	volatile bool isAttached() const;
	volatile bool findAuthWin();
	volatile bool findCloseAllCallWin();
	volatile bool available() const;
	volatile bool shouldStop() const;
	volatile int timeCount() const;

	//smc actions
	void sleep(unsigned int millSeconds);
	void run();
	void findUIWins();
	void DoAuth();
	void DoCloseAllCallWin();
	void login();
	void logout();
	void dial();
	void resetTimer();
	void timeout();
	void closeProcess();
	void reset();
	void selfAvailable(bool isAvailable);

	// events
	void processClosed();
	void attached(HWND ctrlWin);
	void callingFinished();
	void stop();

	// notify
	void logined();
	void onlined();
	void clientRefused();
	void clientFailed();
	void clientBusy();
	void clientMissed();
	void clientFinished();

private:
	volatile HWND m_uiWinMainHandle;
	volatile HWND m_ctrlWinHandle;
	volatile HWND m_authWinHandle;
	volatile HWND m_closeAllCallWinHandle;
	volatile DWORD m_procId;

	ClientStateContext m_clientContext;

	ClientsManager* m_manager;

	string m_userName;
	string m_password;
	string m_calleeId;

	volatile bool m_selfAvailable;
	volatile bool m_isInDialing;
	volatile bool m_shouldStop;

	volatile time_t m_taskStartTime;
	static int m_timeOutSecondCount;

	CRITICAL_SECTION m_contextCriticalSection;
	HANDLE m_watchDogThreadHandle;
	HANDLE m_workThreadHandle;
	int m_sleepTime;
	friend DWORD WINAPI WatchDog(LPVOID lpParameter);
	friend DWORD WINAPI WorkThread(LPVOID lpParameter);

};
